<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>URL dispatcher &mdash; Django 1.7.8.dev20150401230226 documentation</title>
    
    <link rel="stylesheet" href="../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../',
        VERSION:     '1.7.8.dev20150401230226',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <link rel="top" title="Django 1.7.8.dev20150401230226 documentation" href="../../index.html" />
    <link rel="up" title="Handling HTTP requests" href="index.html" />
    <link rel="next" title="Writing views" href="views.html" />
    <link rel="prev" title="Handling HTTP requests" href="index.html" />



 
<script type="text/javascript" src="../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);
</script>


  </head>
  <body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../index.html">Django 1.7.8.dev20150401230226 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../index.html">Home</a>  |
        <a title="Table of contents" href="../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../genindex.html">Index</a>  |
        <a title="Module index" href="../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="index.html" title="Handling HTTP requests">previous</a>
     |
    <a href="../index.html" title="Using Django" accesskey="U">up</a>
   |
    <a href="views.html" title="Writing views">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="topics-http-urls">
            
  <div class="section" id="s-url-dispatcher">
<span id="url-dispatcher"></span><h1>URL dispatcher<a class="headerlink" href="#url-dispatcher" title="Permalink to this headline">¶</a></h1>
<p>A clean, elegant URL scheme is an important detail in a high-quality Web
application. Django lets you design URLs however you want, with no framework
limitations.</p>
<p>There&#8217;s no <tt class="docutils literal"><span class="pre">.php</span></tt> or <tt class="docutils literal"><span class="pre">.cgi</span></tt> required, and certainly none of that
<tt class="docutils literal"><span class="pre">0,2097,1-1-1928,00</span></tt> nonsense.</p>
<p>See <a class="reference external" href="http://www.w3.org/Provider/Style/URI">Cool URIs don&#8217;t change</a>, by World Wide Web creator Tim Berners-Lee, for
excellent arguments on why URLs should be clean and usable.</p>
<div class="section" id="s-overview">
<span id="overview"></span><h2>Overview<a class="headerlink" href="#overview" title="Permalink to this headline">¶</a></h2>
<p>To design URLs for an app, you create a Python module informally called a
<strong>URLconf</strong> (URL configuration). This module is pure Python code and is a
simple mapping between URL patterns (simple regular expressions) to Python
functions (your views).</p>
<p>This mapping can be as short or as long as needed. It can reference other
mappings. And, because it&#8217;s pure Python code, it can be constructed
dynamically.</p>
<p>Django also provides a way to translate URLs according to the active
language. See the <a class="reference internal" href="../i18n/translation.html#url-internationalization"><em>internationalization documentation</em></a> for more information.</p>
</div>
<div class="section" id="s-how-django-processes-a-request">
<span id="s-id1"></span><span id="how-django-processes-a-request"></span><span id="id1"></span><h2>How Django processes a request<a class="headerlink" href="#how-django-processes-a-request" title="Permalink to this headline">¶</a></h2>
<p>When a user requests a page from your Django-powered site, this is the
algorithm the system follows to determine which Python code to execute:</p>
<ol class="arabic simple">
<li>Django determines the root URLconf module to use. Ordinarily,
this is the value of the <a class="reference internal" href="../../ref/settings.html#std:setting-ROOT_URLCONF"><tt class="xref std std-setting docutils literal"><span class="pre">ROOT_URLCONF</span></tt></a> setting, but if the incoming
<tt class="docutils literal"><span class="pre">HttpRequest</span></tt> object has an attribute called <tt class="docutils literal"><span class="pre">urlconf</span></tt> (set by
middleware <a class="reference internal" href="middleware.html#request-middleware"><em>request processing</em></a>), its value
will be used in place of the <a class="reference internal" href="../../ref/settings.html#std:setting-ROOT_URLCONF"><tt class="xref std std-setting docutils literal"><span class="pre">ROOT_URLCONF</span></tt></a> setting.</li>
<li>Django loads that Python module and looks for the variable
<tt class="docutils literal"><span class="pre">urlpatterns</span></tt>. This should be a Python list, in the format returned by
the function <a class="reference internal" href="../../ref/urls.html#django.conf.urls.patterns" title="django.conf.urls.patterns"><tt class="xref py py-func docutils literal"><span class="pre">django.conf.urls.patterns()</span></tt></a>.</li>
<li>Django runs through each URL pattern, in order, and stops at the first
one that matches the requested URL.</li>
<li>Once one of the regexes matches, Django imports and calls the given view,
which is a simple Python function (or a <a class="reference internal" href="../class-based-views/index.html"><em>class based view</em></a>). The view gets passed the following
arguments:<ul>
<li>An instance of <a class="reference internal" href="../../ref/request-response.html#django.http.HttpRequest" title="django.http.HttpRequest"><tt class="xref py py-class docutils literal"><span class="pre">HttpRequest</span></tt></a>.</li>
<li>If the matched regular expression returned no named groups, then the
matches from the regular expression are provided as positional arguments.</li>
<li>The keyword arguments are made up of any named groups matched by the
regular expression, overridden by any arguments specified in the optional
<tt class="docutils literal"><span class="pre">kwargs</span></tt> argument to <a class="reference internal" href="../../ref/urls.html#django.conf.urls.url" title="django.conf.urls.url"><tt class="xref py py-func docutils literal"><span class="pre">django.conf.urls.url()</span></tt></a>.</li>
</ul>
</li>
<li>If no regex matches, or if an exception is raised during any
point in this process, Django invokes an appropriate
error-handling view. See <a class="reference internal" href="#error-handling">Error handling</a> below.</li>
</ol>
</div>
<div class="section" id="s-example">
<span id="example"></span><h2>Example<a class="headerlink" href="#example" title="Permalink to this headline">¶</a></h2>
<p>Here&#8217;s a sample URLconf:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.conf.urls</span> <span class="kn">import</span> <span class="n">patterns</span><span class="p">,</span> <span class="n">url</span>

<span class="kn">from</span> <span class="nn">.</span> <span class="kn">import</span> <span class="n">views</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">&#39;&#39;</span><span class="p">,</span>
    <span class="n">url</span><span class="p">(</span><span class="s">r&#39;^articles/2003/$&#39;</span><span class="p">,</span> <span class="n">views</span><span class="o">.</span><span class="n">special_case_2003</span><span class="p">),</span>
    <span class="n">url</span><span class="p">(</span><span class="s">r&#39;^articles/(\d{4})/$&#39;</span><span class="p">,</span> <span class="n">views</span><span class="o">.</span><span class="n">year_archive</span><span class="p">),</span>
    <span class="n">url</span><span class="p">(</span><span class="s">r&#39;^articles/(\d{4})/(\d{2})/$&#39;</span><span class="p">,</span> <span class="n">views</span><span class="o">.</span><span class="n">month_archive</span><span class="p">),</span>
    <span class="n">url</span><span class="p">(</span><span class="s">r&#39;^articles/(\d{4})/(\d{2})/(\d+)/$&#39;</span><span class="p">,</span> <span class="n">views</span><span class="o">.</span><span class="n">article_detail</span><span class="p">),</span>
<span class="p">)</span>
</pre></div>
</div>
<p>Notes:</p>
<ul class="simple">
<li>To capture a value from the URL, just put parenthesis around it.</li>
<li>There&#8217;s no need to add a leading slash, because every URL has that. For
example, it&#8217;s <tt class="docutils literal"><span class="pre">^articles</span></tt>, not <tt class="docutils literal"><span class="pre">^/articles</span></tt>.</li>
<li>The <tt class="docutils literal"><span class="pre">'r'</span></tt> in front of each regular expression string is optional but
recommended. It tells Python that a string is &#8220;raw&#8221; &#8211; that nothing in
the string should be escaped. See <a class="reference external" href="http://www.diveintopython.net/regular_expressions/street_addresses.html#re.matching.2.3">Dive Into Python&#8217;s explanation</a>.</li>
</ul>
<p>Example requests:</p>
<ul class="simple">
<li>A request to <tt class="docutils literal"><span class="pre">/articles/2005/03/</span></tt> would match the third entry in the
list. Django would call the function
<tt class="docutils literal"><span class="pre">views.month_archive(request,</span> <span class="pre">'2005',</span> <span class="pre">'03')</span></tt>.</li>
<li><tt class="docutils literal"><span class="pre">/articles/2005/3/</span></tt> would not match any URL patterns, because the
third entry in the list requires two digits for the month.</li>
<li><tt class="docutils literal"><span class="pre">/articles/2003/</span></tt> would match the first pattern in the list, not the
second one, because the patterns are tested in order, and the first one
is the first test to pass. Feel free to exploit the ordering to insert
special cases like this.</li>
<li><tt class="docutils literal"><span class="pre">/articles/2003</span></tt> would not match any of these patterns, because each
pattern requires that the URL end with a slash.</li>
<li><tt class="docutils literal"><span class="pre">/articles/2003/03/03/</span></tt> would match the final pattern. Django would call
the function <tt class="docutils literal"><span class="pre">views.article_detail(request,</span> <span class="pre">'2003',</span> <span class="pre">'03',</span> <span class="pre">'03')</span></tt>.</li>
</ul>
</div>
<div class="section" id="s-named-groups">
<span id="named-groups"></span><h2>Named groups<a class="headerlink" href="#named-groups" title="Permalink to this headline">¶</a></h2>
<p>The above example used simple, <em>non-named</em> regular-expression groups (via
parenthesis) to capture bits of the URL and pass them as <em>positional</em> arguments
to a view. In more advanced usage, it&#8217;s possible to use <em>named</em>
regular-expression groups to capture URL bits and pass them as <em>keyword</em>
arguments to a view.</p>
<p>In Python regular expressions, the syntax for named regular-expression groups
is <tt class="docutils literal"><span class="pre">(?P&lt;name&gt;pattern)</span></tt>, where <tt class="docutils literal"><span class="pre">name</span></tt> is the name of the group and
<tt class="docutils literal"><span class="pre">pattern</span></tt> is some pattern to match.</p>
<p>Here&#8217;s the above example URLconf, rewritten to use named groups:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.conf.urls</span> <span class="kn">import</span> <span class="n">patterns</span><span class="p">,</span> <span class="n">url</span>

<span class="kn">from</span> <span class="nn">.</span> <span class="kn">import</span> <span class="n">views</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">&#39;&#39;</span><span class="p">,</span>
    <span class="n">url</span><span class="p">(</span><span class="s">r&#39;^articles/2003/$&#39;</span><span class="p">,</span> <span class="n">views</span><span class="o">.</span><span class="n">special_case_2003</span><span class="p">),</span>
    <span class="n">url</span><span class="p">(</span><span class="s">r&#39;^articles/(?P&lt;year&gt;\d{4})/$&#39;</span><span class="p">,</span> <span class="n">views</span><span class="o">.</span><span class="n">year_archive</span><span class="p">),</span>
    <span class="n">url</span><span class="p">(</span><span class="s">r&#39;^articles/(?P&lt;year&gt;\d{4})/(?P&lt;month&gt;\d{2})/$&#39;</span><span class="p">,</span> <span class="n">views</span><span class="o">.</span><span class="n">month_archive</span><span class="p">),</span>
    <span class="n">url</span><span class="p">(</span><span class="s">r&#39;^articles/(?P&lt;year&gt;\d{4})/(?P&lt;month&gt;\d{2})/(?P&lt;day&gt;\d{2})/$&#39;</span><span class="p">,</span> <span class="n">views</span><span class="o">.</span><span class="n">article_detail</span><span class="p">),</span>
<span class="p">)</span>
</pre></div>
</div>
<p>This accomplishes exactly the same thing as the previous example, with one
subtle difference: The captured values are passed to view functions as keyword
arguments rather than positional arguments. For example:</p>
<ul class="simple">
<li>A request to <tt class="docutils literal"><span class="pre">/articles/2005/03/</span></tt> would call the function
<tt class="docutils literal"><span class="pre">views.month_archive(request,</span> <span class="pre">year='2005',</span> <span class="pre">month='03')</span></tt>, instead
of <tt class="docutils literal"><span class="pre">views.month_archive(request,</span> <span class="pre">'2005',</span> <span class="pre">'03')</span></tt>.</li>
<li>A request to <tt class="docutils literal"><span class="pre">/articles/2003/03/03/</span></tt> would call the function
<tt class="docutils literal"><span class="pre">views.article_detail(request,</span> <span class="pre">year='2003',</span> <span class="pre">month='03',</span> <span class="pre">day='03')</span></tt>.</li>
</ul>
<p>In practice, this means your URLconfs are slightly more explicit and less prone
to argument-order bugs &#8211; and you can reorder the arguments in your views&#8217;
function definitions. Of course, these benefits come at the cost of brevity;
some developers find the named-group syntax ugly and too verbose.</p>
<div class="section" id="s-the-matching-grouping-algorithm">
<span id="the-matching-grouping-algorithm"></span><h3>The matching/grouping algorithm<a class="headerlink" href="#the-matching-grouping-algorithm" title="Permalink to this headline">¶</a></h3>
<p>Here&#8217;s the algorithm the URLconf parser follows, with respect to named groups
vs. non-named groups in a regular expression:</p>
<ol class="arabic simple">
<li>If there are any named arguments, it will use those, ignoring non-named
arguments.</li>
<li>Otherwise, it will pass all non-named arguments as positional arguments.</li>
</ol>
<p>In both cases, any extra keyword arguments that have been given as per <a class="reference internal" href="#passing-extra-options-to-view-functions">Passing
extra options to view functions</a> (below) will also be passed to the view.</p>
</div>
</div>
<div class="section" id="s-what-the-urlconf-searches-against">
<span id="what-the-urlconf-searches-against"></span><h2>What the URLconf searches against<a class="headerlink" href="#what-the-urlconf-searches-against" title="Permalink to this headline">¶</a></h2>
<p>The URLconf searches against the requested URL, as a normal Python string. This
does not include GET or POST parameters, or the domain name.</p>
<p>For example, in a request to <tt class="docutils literal"><span class="pre">http://www.example.com/myapp/</span></tt>, the URLconf
will look for <tt class="docutils literal"><span class="pre">myapp/</span></tt>.</p>
<p>In a request to <tt class="docutils literal"><span class="pre">http://www.example.com/myapp/?page=3</span></tt>, the URLconf will look
for <tt class="docutils literal"><span class="pre">myapp/</span></tt>.</p>
<p>The URLconf doesn&#8217;t look at the request method. In other words, all request
methods &#8211; <tt class="docutils literal"><span class="pre">POST</span></tt>, <tt class="docutils literal"><span class="pre">GET</span></tt>, <tt class="docutils literal"><span class="pre">HEAD</span></tt>, etc. &#8211; will be routed to the same
function for the same URL.</p>
</div>
<div class="section" id="s-captured-arguments-are-always-strings">
<span id="captured-arguments-are-always-strings"></span><h2>Captured arguments are always strings<a class="headerlink" href="#captured-arguments-are-always-strings" title="Permalink to this headline">¶</a></h2>
<p>Each captured argument is sent to the view as a plain Python string, regardless
of what sort of match the regular expression makes. For example, in this
URLconf line:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">url</span><span class="p">(</span><span class="s">r&#39;^articles/(?P&lt;year&gt;\d{4})/$&#39;</span><span class="p">,</span> <span class="n">views</span><span class="o">.</span><span class="n">year_archive</span><span class="p">),</span>
</pre></div>
</div>
<p>...the <tt class="docutils literal"><span class="pre">year</span></tt> argument to <tt class="docutils literal"><span class="pre">views.year_archive()</span></tt> will be a string, not
an integer, even though the <tt class="docutils literal"><span class="pre">\d{4}</span></tt> will only match integer strings.</p>
</div>
<div class="section" id="s-specifying-defaults-for-view-arguments">
<span id="specifying-defaults-for-view-arguments"></span><h2>Specifying defaults for view arguments<a class="headerlink" href="#specifying-defaults-for-view-arguments" title="Permalink to this headline">¶</a></h2>
<p>A convenient trick is to specify default parameters for your views&#8217; arguments.
Here&#8217;s an example URLconf and view:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># URLconf</span>
<span class="kn">from</span> <span class="nn">django.conf.urls</span> <span class="kn">import</span> <span class="n">patterns</span><span class="p">,</span> <span class="n">url</span>

<span class="kn">from</span> <span class="nn">.</span> <span class="kn">import</span> <span class="n">views</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">&#39;&#39;</span><span class="p">,</span>
    <span class="n">url</span><span class="p">(</span><span class="s">r&#39;^blog/$&#39;</span><span class="p">,</span> <span class="n">views</span><span class="o">.</span><span class="n">page</span><span class="p">),</span>
    <span class="n">url</span><span class="p">(</span><span class="s">r&#39;^blog/page(?P&lt;num&gt;\d+)/$&#39;</span><span class="p">,</span> <span class="n">views</span><span class="o">.</span><span class="n">page</span><span class="p">),</span>
<span class="p">)</span>

<span class="c"># View (in blog/views.py)</span>
<span class="k">def</span> <span class="nf">page</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">num</span><span class="o">=</span><span class="s">&quot;1&quot;</span><span class="p">):</span>
    <span class="c"># Output the appropriate page of blog entries, according to num.</span>
    <span class="o">...</span>
</pre></div>
</div>
<p>In the above example, both URL patterns point to the same view &#8211;
<tt class="docutils literal"><span class="pre">views.page</span></tt> &#8211; but the first pattern doesn&#8217;t capture anything from the
URL. If the first pattern matches, the <tt class="docutils literal"><span class="pre">page()</span></tt> function will use its
default argument for <tt class="docutils literal"><span class="pre">num</span></tt>, <tt class="docutils literal"><span class="pre">&quot;1&quot;</span></tt>. If the second pattern matches,
<tt class="docutils literal"><span class="pre">page()</span></tt> will use whatever <tt class="docutils literal"><span class="pre">num</span></tt> value was captured by the regex.</p>
</div>
<div class="section" id="s-performance">
<span id="performance"></span><h2>Performance<a class="headerlink" href="#performance" title="Permalink to this headline">¶</a></h2>
<p>Each regular expression in a <tt class="docutils literal"><span class="pre">urlpatterns</span></tt> is compiled the first time it&#8217;s
accessed. This makes the system blazingly fast.</p>
</div>
<div class="section" id="s-syntax-of-the-urlpatterns-variable">
<span id="syntax-of-the-urlpatterns-variable"></span><h2>Syntax of the urlpatterns variable<a class="headerlink" href="#syntax-of-the-urlpatterns-variable" title="Permalink to this headline">¶</a></h2>
<p><tt class="docutils literal"><span class="pre">urlpatterns</span></tt> should be a Python list, in the format returned by the function
<a class="reference internal" href="../../ref/urls.html#django.conf.urls.patterns" title="django.conf.urls.patterns"><tt class="xref py py-func docutils literal"><span class="pre">django.conf.urls.patterns()</span></tt></a>. Always use <tt class="docutils literal"><span class="pre">patterns()</span></tt> to create
the <tt class="docutils literal"><span class="pre">urlpatterns</span></tt> variable.</p>
</div>
<div class="section" id="s-error-handling">
<span id="error-handling"></span><h2>Error handling<a class="headerlink" href="#error-handling" title="Permalink to this headline">¶</a></h2>
<p>When Django can&#8217;t find a regex matching the requested URL, or when an
exception is raised, Django will invoke an error-handling view.</p>
<p>The views to use for these cases are specified by four variables. Their
default values should suffice for most projects, but further customization is
possible by assigning values to them.</p>
<p>See the documentation on <a class="reference internal" href="views.html#customizing-error-views"><em>customizing error views</em></a> for the full details.</p>
<p>Such values can be set in your root URLconf. Setting these variables in any
other URLconf will have no effect.</p>
<p>Values must be callables, or strings representing the full Python import path
to the view that should be called to handle the error condition at hand.</p>
<p>The variables are:</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">handler404</span></tt> &#8211; See <a class="reference internal" href="../../ref/urls.html#django.conf.urls.handler404" title="django.conf.urls.handler404"><tt class="xref py py-data docutils literal"><span class="pre">django.conf.urls.handler404</span></tt></a>.</li>
<li><tt class="docutils literal"><span class="pre">handler500</span></tt> &#8211; See <a class="reference internal" href="../../ref/urls.html#django.conf.urls.handler500" title="django.conf.urls.handler500"><tt class="xref py py-data docutils literal"><span class="pre">django.conf.urls.handler500</span></tt></a>.</li>
<li><tt class="docutils literal"><span class="pre">handler403</span></tt> &#8211; See <a class="reference internal" href="../../ref/urls.html#django.conf.urls.handler403" title="django.conf.urls.handler403"><tt class="xref py py-data docutils literal"><span class="pre">django.conf.urls.handler403</span></tt></a>.</li>
<li><tt class="docutils literal"><span class="pre">handler400</span></tt> &#8211; See <a class="reference internal" href="../../ref/urls.html#django.conf.urls.handler400" title="django.conf.urls.handler400"><tt class="xref py py-data docutils literal"><span class="pre">django.conf.urls.handler400</span></tt></a>.</li>
</ul>
</div>
<div class="section" id="s-passing-strings-instead-of-callable-objects">
<span id="passing-strings-instead-of-callable-objects"></span><h2>Passing strings instead of callable objects<a class="headerlink" href="#passing-strings-instead-of-callable-objects" title="Permalink to this headline">¶</a></h2>
<p>It is possible to pass a string containing the path to a view rather than the
actual Python function object. This alternative is supported for the time
being, though is not recommended and will be removed in a future version of
Django.</p>
<p>For example, given this URLconf using Python function objects:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.conf.urls</span> <span class="kn">import</span> <span class="n">patterns</span><span class="p">,</span> <span class="n">url</span>
<span class="kn">from</span> <span class="nn">mysite.views</span> <span class="kn">import</span> <span class="n">archive</span><span class="p">,</span> <span class="n">about</span><span class="p">,</span> <span class="n">contact</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">&#39;&#39;</span><span class="p">,</span>
    <span class="n">url</span><span class="p">(</span><span class="s">r&#39;^archive/$&#39;</span><span class="p">,</span> <span class="n">archive</span><span class="p">),</span>
    <span class="n">url</span><span class="p">(</span><span class="s">r&#39;^about/$&#39;</span><span class="p">,</span> <span class="n">about</span><span class="p">),</span>
    <span class="n">url</span><span class="p">(</span><span class="s">r&#39;^contact/$&#39;</span><span class="p">,</span> <span class="n">contact</span><span class="p">),</span>
<span class="p">)</span>
</pre></div>
</div>
<p>You can accomplish the same thing by passing strings rather than objects:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.conf.urls</span> <span class="kn">import</span> <span class="n">patterns</span><span class="p">,</span> <span class="n">url</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">&#39;&#39;</span><span class="p">,</span>
    <span class="n">url</span><span class="p">(</span><span class="s">r&#39;^archive/$&#39;</span><span class="p">,</span> <span class="s">&#39;mysite.views.archive&#39;</span><span class="p">),</span>
    <span class="n">url</span><span class="p">(</span><span class="s">r&#39;^about/$&#39;</span><span class="p">,</span> <span class="s">&#39;mysite.views.about&#39;</span><span class="p">),</span>
    <span class="n">url</span><span class="p">(</span><span class="s">r&#39;^contact/$&#39;</span><span class="p">,</span> <span class="s">&#39;mysite.views.contact&#39;</span><span class="p">),</span>
<span class="p">)</span>
</pre></div>
</div>
<p>The following example is functionally identical. It&#8217;s just a bit more compact
because it imports the module that contains the views, rather than importing
each view individually:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.conf.urls</span> <span class="kn">import</span> <span class="n">patterns</span><span class="p">,</span> <span class="n">url</span>
<span class="kn">from</span> <span class="nn">mysite</span> <span class="kn">import</span> <span class="n">views</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">&#39;&#39;</span><span class="p">,</span>
    <span class="n">url</span><span class="p">(</span><span class="s">r&#39;^archive/$&#39;</span><span class="p">,</span> <span class="n">views</span><span class="o">.</span><span class="n">archive</span><span class="p">),</span>
    <span class="n">url</span><span class="p">(</span><span class="s">r&#39;^about/$&#39;</span><span class="p">,</span> <span class="n">views</span><span class="o">.</span><span class="n">about</span><span class="p">),</span>
    <span class="n">url</span><span class="p">(</span><span class="s">r&#39;^contact/$&#39;</span><span class="p">,</span> <span class="n">views</span><span class="o">.</span><span class="n">contact</span><span class="p">),</span>
<span class="p">)</span>
</pre></div>
</div>
<p>Note that <a class="reference internal" href="../class-based-views/index.html"><em>class based views</em></a> must be
imported:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.conf.urls</span> <span class="kn">import</span> <span class="n">patterns</span><span class="p">,</span> <span class="n">url</span>
<span class="kn">from</span> <span class="nn">mysite.views</span> <span class="kn">import</span> <span class="n">ClassBasedView</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">&#39;&#39;</span><span class="p">,</span>
    <span class="n">url</span><span class="p">(</span><span class="s">r&#39;^myview/$&#39;</span><span class="p">,</span> <span class="n">ClassBasedView</span><span class="o">.</span><span class="n">as_view</span><span class="p">()),</span>
<span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="s-the-view-prefix">
<span id="s-urlpatterns-view-prefix"></span><span id="the-view-prefix"></span><span id="urlpatterns-view-prefix"></span><h2>The view prefix<a class="headerlink" href="#the-view-prefix" title="Permalink to this headline">¶</a></h2>
<p>If you do use strings, it is possible to specify a common prefix in your
<tt class="docutils literal"><span class="pre">patterns()</span></tt> call.</p>
<p>Here&#8217;s an example URLconf based on the <a class="reference internal" href="../../intro/overview.html"><em>Django overview</em></a>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.conf.urls</span> <span class="kn">import</span> <span class="n">patterns</span><span class="p">,</span> <span class="n">url</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">&#39;&#39;</span><span class="p">,</span>
    <span class="n">url</span><span class="p">(</span><span class="s">r&#39;^articles/(\d{4})/$&#39;</span><span class="p">,</span> <span class="s">&#39;news.views.year_archive&#39;</span><span class="p">),</span>
    <span class="n">url</span><span class="p">(</span><span class="s">r&#39;^articles/(\d{4})/(\d{2})/$&#39;</span><span class="p">,</span> <span class="s">&#39;news.views.month_archive&#39;</span><span class="p">),</span>
    <span class="n">url</span><span class="p">(</span><span class="s">r&#39;^articles/(\d{4})/(\d{2})/(\d+)/$&#39;</span><span class="p">,</span> <span class="s">&#39;news.views.article_detail&#39;</span><span class="p">),</span>
<span class="p">)</span>
</pre></div>
</div>
<p>In this example, each view has a common prefix &#8211; <tt class="docutils literal"><span class="pre">'news.views'</span></tt>.
Instead of typing that out for each entry in <tt class="docutils literal"><span class="pre">urlpatterns</span></tt>, you can use the
first argument to the <tt class="docutils literal"><span class="pre">patterns()</span></tt> function to specify a prefix to apply to
each view function.</p>
<p>With this in mind, the above example can be written as:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.conf.urls</span> <span class="kn">import</span> <span class="n">patterns</span><span class="p">,</span> <span class="n">url</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">&#39;news.views&#39;</span><span class="p">,</span>
    <span class="n">url</span><span class="p">(</span><span class="s">r&#39;^articles/(\d{4})/$&#39;</span><span class="p">,</span> <span class="s">&#39;year_archive&#39;</span><span class="p">),</span>
    <span class="n">url</span><span class="p">(</span><span class="s">r&#39;^articles/(\d{4})/(\d{2})/$&#39;</span><span class="p">,</span> <span class="s">&#39;month_archive&#39;</span><span class="p">),</span>
    <span class="n">url</span><span class="p">(</span><span class="s">r&#39;^articles/(\d{4})/(\d{2})/(\d+)/$&#39;</span><span class="p">,</span> <span class="s">&#39;article_detail&#39;</span><span class="p">),</span>
<span class="p">)</span>
</pre></div>
</div>
<p>Note that you don&#8217;t put a trailing dot (<tt class="docutils literal"><span class="pre">&quot;.&quot;</span></tt>) in the prefix. Django puts
that in automatically.</p>
<div class="section" id="s-multiple-view-prefixes">
<span id="multiple-view-prefixes"></span><h3>Multiple view prefixes<a class="headerlink" href="#multiple-view-prefixes" title="Permalink to this headline">¶</a></h3>
<p>In practice, you&#8217;ll probably end up mixing and matching views to the point
where the views in your <tt class="docutils literal"><span class="pre">urlpatterns</span></tt> won&#8217;t have a common prefix. However,
you can still take advantage of the view prefix shortcut to remove duplication.
Just add multiple <tt class="docutils literal"><span class="pre">patterns()</span></tt> objects together, like this:</p>
<p>Old:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.conf.urls</span> <span class="kn">import</span> <span class="n">patterns</span><span class="p">,</span> <span class="n">url</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">&#39;&#39;</span><span class="p">,</span>
    <span class="n">url</span><span class="p">(</span><span class="s">r&#39;^$&#39;</span><span class="p">,</span> <span class="s">&#39;myapp.views.app_index&#39;</span><span class="p">),</span>
    <span class="n">url</span><span class="p">(</span><span class="s">r&#39;^(?P&lt;year&gt;\d{4})/(?P&lt;month&gt;[a-z]{3})/$&#39;</span><span class="p">,</span> <span class="s">&#39;myapp.views.month_display&#39;</span><span class="p">),</span>
    <span class="n">url</span><span class="p">(</span><span class="s">r&#39;^tag/(?P&lt;tag&gt;\w+)/$&#39;</span><span class="p">,</span> <span class="s">&#39;weblog.views.tag&#39;</span><span class="p">),</span>
<span class="p">)</span>
</pre></div>
</div>
<p>New:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.conf.urls</span> <span class="kn">import</span> <span class="n">patterns</span><span class="p">,</span> <span class="n">url</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">&#39;myapp.views&#39;</span><span class="p">,</span>
    <span class="n">url</span><span class="p">(</span><span class="s">r&#39;^$&#39;</span><span class="p">,</span> <span class="s">&#39;app_index&#39;</span><span class="p">),</span>
    <span class="n">url</span><span class="p">(</span><span class="s">r&#39;^(?P&lt;year&gt;\d{4})/(?P&lt;month&gt;[a-z]{3})/$&#39;</span><span class="p">,</span><span class="s">&#39;month_display&#39;</span><span class="p">),</span>
<span class="p">)</span>

<span class="n">urlpatterns</span> <span class="o">+=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">&#39;weblog.views&#39;</span><span class="p">,</span>
    <span class="n">url</span><span class="p">(</span><span class="s">r&#39;^tag/(?P&lt;tag&gt;\w+)/$&#39;</span><span class="p">,</span> <span class="s">&#39;tag&#39;</span><span class="p">),</span>
<span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="s-including-other-urlconfs">
<span id="s-id2"></span><span id="including-other-urlconfs"></span><span id="id2"></span><h2>Including other URLconfs<a class="headerlink" href="#including-other-urlconfs" title="Permalink to this headline">¶</a></h2>
<p>At any point, your <tt class="docutils literal"><span class="pre">urlpatterns</span></tt> can &#8220;include&#8221; other URLconf modules. This
essentially &#8220;roots&#8221; a set of URLs below other ones.</p>
<p>For example, here&#8217;s an excerpt of the URLconf for the <a class="reference external" href="https://www.djangoproject.com/">Django Web site</a>
itself. It includes a number of other URLconfs:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.conf.urls</span> <span class="kn">import</span> <span class="n">include</span><span class="p">,</span> <span class="n">patterns</span><span class="p">,</span> <span class="n">url</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">&#39;&#39;</span><span class="p">,</span>
    <span class="c"># ... snip ...</span>
    <span class="n">url</span><span class="p">(</span><span class="s">r&#39;^comments/&#39;</span><span class="p">,</span> <span class="n">include</span><span class="p">(</span><span class="s">&#39;django.contrib.comments.urls&#39;</span><span class="p">)),</span>
    <span class="n">url</span><span class="p">(</span><span class="s">r&#39;^community/&#39;</span><span class="p">,</span> <span class="n">include</span><span class="p">(</span><span class="s">&#39;django_website.aggregator.urls&#39;</span><span class="p">)),</span>
    <span class="n">url</span><span class="p">(</span><span class="s">r&#39;^contact/&#39;</span><span class="p">,</span> <span class="n">include</span><span class="p">(</span><span class="s">&#39;django_website.contact.urls&#39;</span><span class="p">)),</span>
    <span class="c"># ... snip ...</span>
<span class="p">)</span>
</pre></div>
</div>
<p>Note that the regular expressions in this example don&#8217;t have a <tt class="docutils literal"><span class="pre">$</span></tt>
(end-of-string match character) but do include a trailing slash. Whenever
Django encounters <tt class="docutils literal"><span class="pre">include()</span></tt> (<a class="reference internal" href="../../ref/urls.html#django.conf.urls.include" title="django.conf.urls.include"><tt class="xref py py-func docutils literal"><span class="pre">django.conf.urls.include()</span></tt></a>), it chops
off whatever part of the URL matched up to that point and sends the remaining
string to the included URLconf for further processing.</p>
<p>Another possibility is to include additional URL patterns not by specifying the
URLconf Python module defining them as the <tt class="docutils literal"><span class="pre">include()</span></tt> argument but by using
directly the pattern list as returned by <a class="reference internal" href="../../ref/urls.html#django.conf.urls.patterns" title="django.conf.urls.patterns"><tt class="xref py py-func docutils literal"><span class="pre">patterns()</span></tt></a>
instead. For example, consider this URLconf:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.conf.urls</span> <span class="kn">import</span> <span class="n">include</span><span class="p">,</span> <span class="n">patterns</span><span class="p">,</span> <span class="n">url</span>

<span class="kn">from</span> <span class="nn">apps.main</span> <span class="kn">import</span> <span class="n">views</span> <span class="k">as</span> <span class="n">main_views</span>
<span class="kn">from</span> <span class="nn">credit</span> <span class="kn">import</span> <span class="n">views</span> <span class="k">as</span> <span class="n">credit_views</span>

<span class="n">extra_patterns</span> <span class="o">=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">&#39;&#39;</span><span class="p">,</span>
    <span class="n">url</span><span class="p">(</span><span class="s">r&#39;^reports/(?P&lt;id&gt;\d+)/$&#39;</span><span class="p">,</span> <span class="n">credit_views</span><span class="o">.</span><span class="n">report</span><span class="p">),</span>
    <span class="n">url</span><span class="p">(</span><span class="s">r&#39;^charge/$&#39;</span><span class="p">,</span> <span class="n">credit_views</span><span class="o">.</span><span class="n">charge</span><span class="p">),</span>
<span class="p">)</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">&#39;&#39;</span><span class="p">,</span>
    <span class="n">url</span><span class="p">(</span><span class="s">r&#39;^$&#39;</span><span class="p">,</span> <span class="n">main_views</span><span class="o">.</span><span class="n">homepage</span><span class="p">),</span>
    <span class="n">url</span><span class="p">(</span><span class="s">r&#39;^help/&#39;</span><span class="p">,</span> <span class="n">include</span><span class="p">(</span><span class="s">&#39;apps.help.urls&#39;</span><span class="p">)),</span>
    <span class="n">url</span><span class="p">(</span><span class="s">r&#39;^credit/&#39;</span><span class="p">,</span> <span class="n">include</span><span class="p">(</span><span class="n">extra_patterns</span><span class="p">)),</span>
<span class="p">)</span>
</pre></div>
</div>
<p>In this example, the <tt class="docutils literal"><span class="pre">/credit/reports/</span></tt> URL will be handled by the
<tt class="docutils literal"><span class="pre">credit.views.report()</span></tt> Django view.</p>
<p>This can be used to remove redundancy from URLconfs where a single pattern
prefix is used repeatedly. For example, consider this URLconf:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.conf.urls</span> <span class="kn">import</span> <span class="n">patterns</span><span class="p">,</span> <span class="n">url</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">&#39;wiki.views&#39;</span><span class="p">,</span>
    <span class="n">url</span><span class="p">(</span><span class="s">r&#39;^(?P&lt;page_slug&gt;\w+)-(?P&lt;page_id&gt;\w+)/history/$&#39;</span><span class="p">,</span> <span class="s">&#39;history&#39;</span><span class="p">),</span>
    <span class="n">url</span><span class="p">(</span><span class="s">r&#39;^(?P&lt;page_slug&gt;\w+)-(?P&lt;page_id&gt;\w+)/edit/$&#39;</span><span class="p">,</span> <span class="s">&#39;edit&#39;</span><span class="p">),</span>
    <span class="n">url</span><span class="p">(</span><span class="s">r&#39;^(?P&lt;page_slug&gt;\w+)-(?P&lt;page_id&gt;\w+)/discuss/$&#39;</span><span class="p">,</span> <span class="s">&#39;discuss&#39;</span><span class="p">),</span>
    <span class="n">url</span><span class="p">(</span><span class="s">r&#39;^(?P&lt;page_slug&gt;\w+)-(?P&lt;page_id&gt;\w+)/permissions/$&#39;</span><span class="p">,</span> <span class="s">&#39;permissions&#39;</span><span class="p">),</span>
<span class="p">)</span>
</pre></div>
</div>
<p>We can improve this by stating the common path prefix only once and grouping
the suffixes that differ:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.conf.urls</span> <span class="kn">import</span> <span class="n">include</span><span class="p">,</span> <span class="n">patterns</span><span class="p">,</span> <span class="n">url</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">&#39;&#39;</span><span class="p">,</span>
    <span class="n">url</span><span class="p">(</span><span class="s">r&#39;^(?P&lt;page_slug&gt;\w+)-(?P&lt;page_id&gt;\w+)/&#39;</span><span class="p">,</span> <span class="n">include</span><span class="p">(</span><span class="n">patterns</span><span class="p">(</span><span class="s">&#39;wiki.views&#39;</span><span class="p">,</span>
        <span class="n">url</span><span class="p">(</span><span class="s">r&#39;^history/$&#39;</span><span class="p">,</span> <span class="s">&#39;history&#39;</span><span class="p">),</span>
        <span class="n">url</span><span class="p">(</span><span class="s">r&#39;^edit/$&#39;</span><span class="p">,</span> <span class="s">&#39;edit&#39;</span><span class="p">),</span>
        <span class="n">url</span><span class="p">(</span><span class="s">r&#39;^discuss/$&#39;</span><span class="p">,</span> <span class="s">&#39;discuss&#39;</span><span class="p">),</span>
        <span class="n">url</span><span class="p">(</span><span class="s">r&#39;^permissions/$&#39;</span><span class="p">,</span> <span class="s">&#39;permissions&#39;</span><span class="p">),</span>
    <span class="p">))),</span>
<span class="p">)</span>
</pre></div>
</div>
<div class="section" id="s-captured-parameters">
<span id="captured-parameters"></span><h3>Captured parameters<a class="headerlink" href="#captured-parameters" title="Permalink to this headline">¶</a></h3>
<p>An included URLconf receives any captured parameters from parent URLconfs, so
the following example is valid:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># In settings/urls/main.py</span>
<span class="kn">from</span> <span class="nn">django.conf.urls</span> <span class="kn">import</span> <span class="n">include</span><span class="p">,</span> <span class="n">patterns</span><span class="p">,</span> <span class="n">url</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">&#39;&#39;</span><span class="p">,</span>
    <span class="n">url</span><span class="p">(</span><span class="s">r&#39;^(?P&lt;username&gt;\w+)/blog/&#39;</span><span class="p">,</span> <span class="n">include</span><span class="p">(</span><span class="s">&#39;foo.urls.blog&#39;</span><span class="p">)),</span>
<span class="p">)</span>

<span class="c"># In foo/urls/blog.py</span>
<span class="kn">from</span> <span class="nn">django.conf.urls</span> <span class="kn">import</span> <span class="n">patterns</span><span class="p">,</span> <span class="n">url</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">&#39;foo.views&#39;</span><span class="p">,</span>
    <span class="n">url</span><span class="p">(</span><span class="s">r&#39;^$&#39;</span><span class="p">,</span> <span class="s">&#39;blog.index&#39;</span><span class="p">),</span>
    <span class="n">url</span><span class="p">(</span><span class="s">r&#39;^archive/$&#39;</span><span class="p">,</span> <span class="s">&#39;blog.archive&#39;</span><span class="p">),</span>
<span class="p">)</span>
</pre></div>
</div>
<p>In the above example, the captured <tt class="docutils literal"><span class="pre">&quot;username&quot;</span></tt> variable is passed to the
included URLconf, as expected.</p>
</div>
</div>
<div class="section" id="s-passing-extra-options-to-view-functions">
<span id="s-views-extra-options"></span><span id="passing-extra-options-to-view-functions"></span><span id="views-extra-options"></span><h2>Passing extra options to view functions<a class="headerlink" href="#passing-extra-options-to-view-functions" title="Permalink to this headline">¶</a></h2>
<p>URLconfs have a hook that lets you pass extra arguments to your view functions,
as a Python dictionary.</p>
<p>The <a class="reference internal" href="../../ref/urls.html#django.conf.urls.url" title="django.conf.urls.url"><tt class="xref py py-func docutils literal"><span class="pre">django.conf.urls.url()</span></tt></a> function can take an optional third argument
which should be a dictionary of extra keyword arguments to pass to the view
function.</p>
<p>For example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.conf.urls</span> <span class="kn">import</span> <span class="n">patterns</span><span class="p">,</span> <span class="n">url</span>
<span class="kn">from</span> <span class="nn">.</span> <span class="kn">import</span> <span class="n">views</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">&#39;&#39;</span><span class="p">,</span>
    <span class="n">url</span><span class="p">(</span><span class="s">r&#39;^blog/(?P&lt;year&gt;\d{4})/$&#39;</span><span class="p">,</span> <span class="n">views</span><span class="o">.</span><span class="n">year_archive</span><span class="p">,</span> <span class="p">{</span><span class="s">&#39;foo&#39;</span><span class="p">:</span> <span class="s">&#39;bar&#39;</span><span class="p">}),</span>
<span class="p">)</span>
</pre></div>
</div>
<p>In this example, for a request to <tt class="docutils literal"><span class="pre">/blog/2005/</span></tt>, Django will call
<tt class="docutils literal"><span class="pre">views.year_archive(request,</span> <span class="pre">year='2005',</span> <span class="pre">foo='bar')</span></tt>.</p>
<p>This technique is used in the
<a class="reference internal" href="../../ref/contrib/syndication.html"><em>syndication framework</em></a> to pass metadata and
options to views.</p>
<div class="admonition-dealing-with-conflicts admonition">
<p class="first admonition-title">Dealing with conflicts</p>
<p class="last">It&#8217;s possible to have a URL pattern which captures named keyword arguments,
and also passes arguments with the same names in its dictionary of extra
arguments. When this happens, the arguments in the dictionary will be used
instead of the arguments captured in the URL.</p>
</div>
<div class="section" id="s-passing-extra-options-to-include">
<span id="passing-extra-options-to-include"></span><h3>Passing extra options to <tt class="docutils literal"><span class="pre">include()</span></tt><a class="headerlink" href="#passing-extra-options-to-include" title="Permalink to this headline">¶</a></h3>
<p>Similarly, you can pass extra options to <a class="reference internal" href="../../ref/urls.html#django.conf.urls.include" title="django.conf.urls.include"><tt class="xref py py-func docutils literal"><span class="pre">include()</span></tt></a>.
When you pass extra options to <tt class="docutils literal"><span class="pre">include()</span></tt>, <em>each</em> line in the included
URLconf will be passed the extra options.</p>
<p>For example, these two URLconf sets are functionally identical:</p>
<p>Set one:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># main.py</span>
<span class="kn">from</span> <span class="nn">django.conf.urls</span> <span class="kn">import</span> <span class="n">include</span><span class="p">,</span> <span class="n">patterns</span><span class="p">,</span> <span class="n">url</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">&#39;&#39;</span><span class="p">,</span>
    <span class="n">url</span><span class="p">(</span><span class="s">r&#39;^blog/&#39;</span><span class="p">,</span> <span class="n">include</span><span class="p">(</span><span class="s">&#39;inner&#39;</span><span class="p">),</span> <span class="p">{</span><span class="s">&#39;blogid&#39;</span><span class="p">:</span> <span class="mi">3</span><span class="p">}),</span>
<span class="p">)</span>

<span class="c"># inner.py</span>
<span class="kn">from</span> <span class="nn">django.conf.urls</span> <span class="kn">import</span> <span class="n">patterns</span><span class="p">,</span> <span class="n">url</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">&#39;&#39;</span><span class="p">,</span>
    <span class="n">url</span><span class="p">(</span><span class="s">r&#39;^archive/$&#39;</span><span class="p">,</span> <span class="s">&#39;mysite.views.archive&#39;</span><span class="p">),</span>
    <span class="n">url</span><span class="p">(</span><span class="s">r&#39;^about/$&#39;</span><span class="p">,</span> <span class="s">&#39;mysite.views.about&#39;</span><span class="p">),</span>
<span class="p">)</span>
</pre></div>
</div>
<p>Set two:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># main.py</span>
<span class="kn">from</span> <span class="nn">django.conf.urls</span> <span class="kn">import</span> <span class="n">include</span><span class="p">,</span> <span class="n">patterns</span><span class="p">,</span> <span class="n">url</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">&#39;&#39;</span><span class="p">,</span>
    <span class="n">url</span><span class="p">(</span><span class="s">r&#39;^blog/&#39;</span><span class="p">,</span> <span class="n">include</span><span class="p">(</span><span class="s">&#39;inner&#39;</span><span class="p">)),</span>
<span class="p">)</span>

<span class="c"># inner.py</span>
<span class="kn">from</span> <span class="nn">django.conf.urls</span> <span class="kn">import</span> <span class="n">patterns</span><span class="p">,</span> <span class="n">url</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">&#39;&#39;</span><span class="p">,</span>
    <span class="n">url</span><span class="p">(</span><span class="s">r&#39;^archive/$&#39;</span><span class="p">,</span> <span class="s">&#39;mysite.views.archive&#39;</span><span class="p">,</span> <span class="p">{</span><span class="s">&#39;blogid&#39;</span><span class="p">:</span> <span class="mi">3</span><span class="p">}),</span>
    <span class="n">url</span><span class="p">(</span><span class="s">r&#39;^about/$&#39;</span><span class="p">,</span> <span class="s">&#39;mysite.views.about&#39;</span><span class="p">,</span> <span class="p">{</span><span class="s">&#39;blogid&#39;</span><span class="p">:</span> <span class="mi">3</span><span class="p">}),</span>
<span class="p">)</span>
</pre></div>
</div>
<p>Note that extra options will <em>always</em> be passed to <em>every</em> line in the included
URLconf, regardless of whether the line&#8217;s view actually accepts those options
as valid. For this reason, this technique is only useful if you&#8217;re certain that
every view in the included URLconf accepts the extra options you&#8217;re passing.</p>
</div>
</div>
<div class="section" id="s-reverse-resolution-of-urls">
<span id="reverse-resolution-of-urls"></span><h2>Reverse resolution of URLs<a class="headerlink" href="#reverse-resolution-of-urls" title="Permalink to this headline">¶</a></h2>
<p>A common need when working on a Django project is the possibility to obtain URLs
in their final forms either for embedding in generated content (views and assets
URLs, URLs shown to the user, etc.) or for handling of the navigation flow on
the server side (redirections, etc.)</p>
<p>It is strongly desirable not having to hard-code these URLs (a laborious,
non-scalable and error-prone strategy) or having to devise ad-hoc mechanisms for
generating URLs that are parallel to the design described by the URLconf and as
such in danger of producing stale URLs at some point.</p>
<p>In other words, what&#8217;s needed is a DRY mechanism. Among other advantages it
would allow evolution of the URL design without having to go all over the
project source code to search and replace outdated URLs.</p>
<p>The piece of information we have available as a starting point to get a URL is
an identification (e.g. the name) of the view in charge of handling it, other
pieces of information that necessarily must participate in the lookup of the
right URL are the types (positional, keyword) and values of the view arguments.</p>
<p>Django provides a solution such that the URL mapper is the only repository of
the URL design. You feed it with your URLconf and then it can be used in both
directions:</p>
<ul class="simple">
<li>Starting with a URL requested by the user/browser, it calls the right Django
view providing any arguments it might need with their values as extracted from
the URL.</li>
<li>Starting with the identification of the corresponding Django view plus the
values of arguments that would be passed to it, obtain the associated URL.</li>
</ul>
<p>The first one is the usage we&#8217;ve been discussing in the previous sections. The
second one is what is known as <em>reverse resolution of URLs</em>, <em>reverse URL
matching</em>, <em>reverse URL lookup</em>, or simply <em>URL reversing</em>.</p>
<p>Django provides tools for performing URL reversing that match the different
layers where URLs are needed:</p>
<ul class="simple">
<li>In templates: Using the <a class="reference internal" href="../../ref/templates/builtins.html#std:templatetag-url"><tt class="xref std std-ttag docutils literal"><span class="pre">url</span></tt></a> template tag.</li>
<li>In Python code: Using the <a class="reference internal" href="../../ref/urlresolvers.html#django.core.urlresolvers.reverse" title="django.core.urlresolvers.reverse"><tt class="xref py py-func docutils literal"><span class="pre">django.core.urlresolvers.reverse()</span></tt></a>
function.</li>
<li>In higher level code related to handling of URLs of Django model instances:
The <a class="reference internal" href="../../ref/models/instances.html#django.db.models.Model.get_absolute_url" title="django.db.models.Model.get_absolute_url"><tt class="xref py py-meth docutils literal"><span class="pre">get_absolute_url()</span></tt></a> method.</li>
</ul>
<div class="section" id="s-examples">
<span id="examples"></span><h3>Examples<a class="headerlink" href="#examples" title="Permalink to this headline">¶</a></h3>
<p>Consider again this URLconf entry:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.conf.urls</span> <span class="kn">import</span> <span class="n">patterns</span><span class="p">,</span> <span class="n">url</span>

<span class="kn">from</span> <span class="nn">.</span> <span class="kn">import</span> <span class="n">views</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">&#39;&#39;</span><span class="p">,</span>
    <span class="c">#...</span>
    <span class="n">url</span><span class="p">(</span><span class="s">r&#39;^articles/(\d{4})/$&#39;</span><span class="p">,</span> <span class="n">views</span><span class="o">.</span><span class="n">year_archive</span><span class="p">),</span>
    <span class="c">#...</span>
<span class="p">)</span>
</pre></div>
</div>
<p>According to this design, the URL for the archive corresponding to year <em>nnnn</em>
is <tt class="docutils literal"><span class="pre">/articles/nnnn/</span></tt>.</p>
<p>You can obtain these in template code by using:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="nt">&lt;a</span> <span class="na">href=</span><span class="s">&quot;</span><span class="cp">{%</span> <span class="k">url</span> <span class="s1">&#39;news.views.year_archive&#39;</span> <span class="m">2012</span> <span class="cp">%}</span><span class="s">&quot;</span><span class="nt">&gt;</span>2012 Archive<span class="nt">&lt;/a&gt;</span>
<span class="c">{# Or with the year in a template context variable: #}</span>
<span class="nt">&lt;ul&gt;</span>
<span class="cp">{%</span> <span class="k">for</span> <span class="nv">yearvar</span> <span class="k">in</span> <span class="nv">year_list</span> <span class="cp">%}</span>
<span class="nt">&lt;li&gt;&lt;a</span> <span class="na">href=</span><span class="s">&quot;</span><span class="cp">{%</span> <span class="k">url</span> <span class="s1">&#39;news.views.year_archive&#39;</span> <span class="nv">yearvar</span> <span class="cp">%}</span><span class="s">&quot;</span><span class="nt">&gt;</span><span class="cp">{{</span> <span class="nv">yearvar</span> <span class="cp">}}</span> Archive<span class="nt">&lt;/a&gt;&lt;/li&gt;</span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
<span class="nt">&lt;/ul&gt;</span>
</pre></div>
</div>
<p>Or in Python code:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.core.urlresolvers</span> <span class="kn">import</span> <span class="n">reverse</span>
<span class="kn">from</span> <span class="nn">django.http</span> <span class="kn">import</span> <span class="n">HttpResponseRedirect</span>

<span class="k">def</span> <span class="nf">redirect_to_year</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
    <span class="c"># ...</span>
    <span class="n">year</span> <span class="o">=</span> <span class="mi">2006</span>
    <span class="c"># ...</span>
    <span class="k">return</span> <span class="n">HttpResponseRedirect</span><span class="p">(</span><span class="n">reverse</span><span class="p">(</span><span class="s">&#39;news.views.year_archive&#39;</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="p">(</span><span class="n">year</span><span class="p">,)))</span>
</pre></div>
</div>
<p>If, for some reason, it was decided that the URLs where content for yearly
article archives are published at should be changed then you would only need to
change the entry in the URLconf.</p>
<p>In some scenarios where views are of a generic nature, a many-to-one
relationship might exist between URLs and views. For these cases the view name
isn&#8217;t a good enough identifier for it when comes the time of reversing
URLs. Read the next section to know about the solution Django provides for this.</p>
</div>
</div>
<div class="section" id="s-naming-url-patterns">
<span id="s-id3"></span><span id="naming-url-patterns"></span><span id="id3"></span><h2>Naming URL patterns<a class="headerlink" href="#naming-url-patterns" title="Permalink to this headline">¶</a></h2>
<p>It&#8217;s fairly common to use the same view function in multiple URL patterns in
your URLconf. For example, these two URL patterns both point to the <tt class="docutils literal"><span class="pre">archive</span></tt>
view:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.conf.urls</span> <span class="kn">import</span> <span class="n">patterns</span><span class="p">,</span> <span class="n">url</span>
<span class="kn">from</span> <span class="nn">mysite.views</span> <span class="kn">import</span> <span class="n">archive</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">&#39;&#39;</span><span class="p">,</span>
    <span class="n">url</span><span class="p">(</span><span class="s">r&#39;^archive/(\d{4})/$&#39;</span><span class="p">,</span> <span class="n">archive</span><span class="p">),</span>
    <span class="n">url</span><span class="p">(</span><span class="s">r&#39;^archive-summary/(\d{4})/$&#39;</span><span class="p">,</span> <span class="n">archive</span><span class="p">,</span> <span class="p">{</span><span class="s">&#39;summary&#39;</span><span class="p">:</span> <span class="bp">True</span><span class="p">}),</span>
<span class="p">)</span>
</pre></div>
</div>
<p>This is completely valid, but it leads to problems when you try to do reverse
URL matching (through the <a class="reference internal" href="../../ref/urlresolvers.html#django.core.urlresolvers.reverse" title="django.core.urlresolvers.reverse"><tt class="xref py py-func docutils literal"><span class="pre">reverse()</span></tt></a> function
or the <a class="reference internal" href="../../ref/templates/builtins.html#std:templatetag-url"><tt class="xref std std-ttag docutils literal"><span class="pre">url</span></tt></a> template tag). Continuing this example, if you wanted to
retrieve the URL for the <tt class="docutils literal"><span class="pre">archive</span></tt> view, Django&#8217;s reverse URL matcher would
get confused, because <em>two</em> URL patterns point at that view.</p>
<p>To solve this problem, Django supports <strong>named URL patterns</strong>. That is, you can
give a name to a URL pattern in order to distinguish it from other patterns
using the same view and parameters. Then, you can use this name in reverse URL
matching.</p>
<p>Here&#8217;s the above example, rewritten to use named URL patterns:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.conf.urls</span> <span class="kn">import</span> <span class="n">patterns</span><span class="p">,</span> <span class="n">url</span>
<span class="kn">from</span> <span class="nn">mysite.views</span> <span class="kn">import</span> <span class="n">archive</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">&#39;&#39;</span><span class="p">,</span>
    <span class="n">url</span><span class="p">(</span><span class="s">r&#39;^archive/(\d{4})/$&#39;</span><span class="p">,</span> <span class="n">archive</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s">&quot;full-archive&quot;</span><span class="p">),</span>
    <span class="n">url</span><span class="p">(</span><span class="s">r&#39;^archive-summary/(\d{4})/$&#39;</span><span class="p">,</span> <span class="n">archive</span><span class="p">,</span> <span class="p">{</span><span class="s">&#39;summary&#39;</span><span class="p">:</span> <span class="bp">True</span><span class="p">},</span> <span class="n">name</span><span class="o">=</span><span class="s">&quot;arch-summary&quot;</span><span class="p">),</span>
<span class="p">)</span>
</pre></div>
</div>
<p>With these names in place (<tt class="docutils literal"><span class="pre">full-archive</span></tt> and <tt class="docutils literal"><span class="pre">arch-summary</span></tt>), you can
target each pattern individually by using its name:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">url</span> <span class="s1">&#39;arch-summary&#39;</span> <span class="m">1945</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">url</span> <span class="s1">&#39;full-archive&#39;</span> <span class="m">2007</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>Even though both URL patterns refer to the <tt class="docutils literal"><span class="pre">archive</span></tt> view here, using the
<tt class="docutils literal"><span class="pre">name</span></tt> parameter to <a class="reference internal" href="../../ref/urls.html#django.conf.urls.url" title="django.conf.urls.url"><tt class="xref py py-func docutils literal"><span class="pre">django.conf.urls.url()</span></tt></a> allows you to tell them
apart in templates.</p>
<p>The string used for the URL name can contain any characters you like. You are
not restricted to valid Python names.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p>When you name your URL patterns, make sure you use names that are unlikely
to clash with any other application&#8217;s choice of names. If you call your URL
pattern <tt class="docutils literal"><span class="pre">comment</span></tt>, and another application does the same thing, there&#8217;s
no guarantee which URL will be inserted into your template when you use
this name.</p>
<p class="last">Putting a prefix on your URL names, perhaps derived from the application
name, will decrease the chances of collision. We recommend something like
<tt class="docutils literal"><span class="pre">myapp-comment</span></tt> instead of <tt class="docutils literal"><span class="pre">comment</span></tt>.</p>
</div>
</div>
<div class="section" id="s-url-namespaces">
<span id="s-topics-http-defining-url-namespaces"></span><span id="url-namespaces"></span><span id="topics-http-defining-url-namespaces"></span><h2>URL namespaces<a class="headerlink" href="#url-namespaces" title="Permalink to this headline">¶</a></h2>
<div class="section" id="s-introduction">
<span id="introduction"></span><h3>Introduction<a class="headerlink" href="#introduction" title="Permalink to this headline">¶</a></h3>
<p>URL namespaces allow you to uniquely reverse <a class="reference internal" href="#naming-url-patterns"><em>named URL patterns</em></a> even if different applications use the same URL names.
It&#8217;s a good practice for third-party apps to always use namespaced URLs (as we
did in the tutorial). Similarly, it also allows you to reverse URLs if multiple
instances of an application are deployed. In other words, since multiple
instances of a single application will share named URLs, namespaces provide a
way to tell these named URLs apart.</p>
<p>Django applications that make proper use of URL namespacing can be deployed more
than once for a particular site. For example <a class="reference internal" href="../../ref/contrib/admin/index.html#module-django.contrib.admin" title="django.contrib.admin: Django's admin site."><tt class="xref py py-mod docutils literal"><span class="pre">django.contrib.admin</span></tt></a> has an
<a class="reference internal" href="../../ref/contrib/admin/index.html#django.contrib.admin.AdminSite" title="django.contrib.admin.AdminSite"><tt class="xref py py-class docutils literal"><span class="pre">AdminSite</span></tt></a> class which allows you to easily
<a class="reference internal" href="../../ref/contrib/admin/index.html#multiple-admin-sites"><em>deploy more than once instance of the admin</em></a>.
In a later example, we&#8217;ll discuss the idea of deploying the polls application
from the tutorial in two different locations so we can serve the same
functionality to two different audiences (authors and publishers).</p>
<p>A URL namespace comes in two parts, both of which are strings:</p>
<dl class="glossary docutils">
<dt id="term-application-namespace">application namespace</dt>
<dd>This describes the name of the application that is being deployed. Every
instance of a single application will have the same application namespace.
For example, Django&#8217;s admin application has the somewhat predictable
application namespace of <tt class="docutils literal"><span class="pre">'admin'</span></tt>.</dd>
<dt id="term-instance-namespace">instance namespace</dt>
<dd>This identifies a specific instance of an application. Instance namespaces
should be unique across your entire project. However, an instance namespace
can be the same as the application namespace. This is used to specify a
default instance of an application. For example, the default Django admin
instance has an instance namespace of <tt class="docutils literal"><span class="pre">'admin'</span></tt>.</dd>
</dl>
<p>Namespaced URLs are specified using the <tt class="docutils literal"><span class="pre">':'</span></tt> operator. For example, the main
index page of the admin application is referenced using <tt class="docutils literal"><span class="pre">'admin:index'</span></tt>. This
indicates a namespace of <tt class="docutils literal"><span class="pre">'admin'</span></tt>, and a named URL of <tt class="docutils literal"><span class="pre">'index'</span></tt>.</p>
<p>Namespaces can also be nested. The named URL <tt class="docutils literal"><span class="pre">'sports:polls:index'</span></tt> would
look for a pattern named <tt class="docutils literal"><span class="pre">'index'</span></tt> in the namespace <tt class="docutils literal"><span class="pre">'polls'</span></tt> that is itself
defined within the top-level namespace <tt class="docutils literal"><span class="pre">'sports'</span></tt>.</p>
</div>
<div class="section" id="s-reversing-namespaced-urls">
<span id="s-topics-http-reversing-url-namespaces"></span><span id="reversing-namespaced-urls"></span><span id="topics-http-reversing-url-namespaces"></span><h3>Reversing namespaced URLs<a class="headerlink" href="#reversing-namespaced-urls" title="Permalink to this headline">¶</a></h3>
<p>When given a namespaced URL (e.g. <tt class="docutils literal"><span class="pre">'polls:index'</span></tt>) to resolve, Django splits
the fully qualified name into parts and then tries the following lookup:</p>
<ol class="arabic">
<li><p class="first">First, Django looks for a matching <a class="reference internal" href="#term-application-namespace"><em class="xref std std-term">application namespace</em></a> (in this
example, <tt class="docutils literal"><span class="pre">'polls'</span></tt>). This will yield a list of instances of that
application.</p>
</li>
<li><p class="first">If there is a <em>current</em> application defined, Django finds and returns
the URL resolver for that instance. The <em>current</em> application can be
specified as an attribute on the template context - applications that
expect to have multiple deployments should set the <tt class="docutils literal"><span class="pre">current_app</span></tt>
attribute on any <a class="reference internal" href="../../ref/templates/api.html#django.template.Context" title="django.template.Context"><tt class="xref py py-class docutils literal"><span class="pre">Context</span></tt></a> or
<a class="reference internal" href="../../ref/templates/api.html#django.template.RequestContext" title="django.template.RequestContext"><tt class="xref py py-class docutils literal"><span class="pre">RequestContext</span></tt></a> that is used to render a template.</p>
<p>The current application can also be specified manually as an argument
to the <a class="reference internal" href="../../ref/urlresolvers.html#django.core.urlresolvers.reverse" title="django.core.urlresolvers.reverse"><tt class="xref py py-func docutils literal"><span class="pre">reverse()</span></tt></a> function.</p>
</li>
<li><p class="first">If there is no current application. Django looks for a default
application instance. The default application instance is the instance
that has an <a class="reference internal" href="#term-instance-namespace"><em class="xref std std-term">instance namespace</em></a> matching the <a class="reference internal" href="#term-application-namespace"><em class="xref std std-term">application
namespace</em></a> (in this example, an instance of <tt class="docutils literal"><span class="pre">polls</span></tt> called <tt class="docutils literal"><span class="pre">'polls'</span></tt>).</p>
</li>
<li><p class="first">If there is no default application instance, Django will pick the last
deployed instance of the application, whatever its instance name may be.</p>
</li>
<li><p class="first">If the provided namespace doesn&#8217;t match an <a class="reference internal" href="#term-application-namespace"><em class="xref std std-term">application namespace</em></a> in
step 1, Django will attempt a direct lookup of the namespace as an
<a class="reference internal" href="#term-instance-namespace"><em class="xref std std-term">instance namespace</em></a>.</p>
</li>
</ol>
<p>If there are nested namespaces, these steps are repeated for each part of the
namespace until only the view name is unresolved. The view name will then be
resolved into a URL in the namespace that has been found.</p>
<div class="section" id="s-id4">
<span id="id4"></span><h4>Example<a class="headerlink" href="#id4" title="Permalink to this headline">¶</a></h4>
<p>To show this resolution strategy in action, consider an example of two instances
of the <tt class="docutils literal"><span class="pre">polls</span></tt> application from the tutorial: one called <tt class="docutils literal"><span class="pre">'author-polls'</span></tt>
and one called <tt class="docutils literal"><span class="pre">'publisher-polls'</span></tt>. Assume we have enhanced that application
so that it takes the instance namespace into consideration when creating and
displaying polls.</p>
<div class="highlight-python"><div class="snippet-filename">urls.py</div>
<div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.conf.urls</span> <span class="kn">import</span> <span class="n">include</span><span class="p">,</span> <span class="n">patterns</span><span class="p">,</span> <span class="n">url</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">&#39;&#39;</span><span class="p">,</span>
    <span class="n">url</span><span class="p">(</span><span class="s">r&#39;^author-polls/&#39;</span><span class="p">,</span> <span class="n">include</span><span class="p">(</span><span class="s">&#39;polls.urls&#39;</span><span class="p">,</span> <span class="n">namespace</span><span class="o">=</span><span class="s">&#39;author-polls&#39;</span><span class="p">,</span> <span class="n">app_name</span><span class="o">=</span><span class="s">&#39;polls&#39;</span><span class="p">)),</span>
    <span class="n">url</span><span class="p">(</span><span class="s">r&#39;^publisher-polls/&#39;</span><span class="p">,</span> <span class="n">include</span><span class="p">(</span><span class="s">&#39;polls.urls&#39;</span><span class="p">,</span> <span class="n">namespace</span><span class="o">=</span><span class="s">&#39;publisher-polls&#39;</span><span class="p">,</span> <span class="n">app_name</span><span class="o">=</span><span class="s">&#39;polls&#39;</span><span class="p">)),</span>
<span class="p">)</span>
</pre></div>
</div>
<div class="highlight-python"><div class="snippet-filename">polls/urls.py</div>
<div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.conf.urls</span> <span class="kn">import</span> <span class="n">patterns</span><span class="p">,</span> <span class="n">url</span>

<span class="kn">from</span> <span class="nn">.</span> <span class="kn">import</span> <span class="n">views</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">&#39;&#39;</span><span class="p">,</span>
    <span class="n">url</span><span class="p">(</span><span class="s">r&#39;^$&#39;</span><span class="p">,</span> <span class="n">views</span><span class="o">.</span><span class="n">IndexView</span><span class="o">.</span><span class="n">as_view</span><span class="p">(),</span> <span class="n">name</span><span class="o">=</span><span class="s">&#39;index&#39;</span><span class="p">),</span>
    <span class="n">url</span><span class="p">(</span><span class="s">r&#39;^(?P&lt;pk&gt;\d+)/$&#39;</span><span class="p">,</span> <span class="n">views</span><span class="o">.</span><span class="n">DetailView</span><span class="o">.</span><span class="n">as_view</span><span class="p">(),</span> <span class="n">name</span><span class="o">=</span><span class="s">&#39;detail&#39;</span><span class="p">),</span>
    <span class="o">...</span>
<span class="p">)</span>
</pre></div>
</div>
<p>Using this setup, the following lookups are possible:</p>
<ul>
<li><p class="first">If one of the instances is current - say, if we were rendering the detail page
in the instance <tt class="docutils literal"><span class="pre">'author-polls'</span></tt> - <tt class="docutils literal"><span class="pre">'polls:index'</span></tt> will resolve to the
index page of the <tt class="docutils literal"><span class="pre">'author-polls'</span></tt> instance; i.e. both of the following will
result in <tt class="docutils literal"><span class="pre">&quot;/author-polls/&quot;</span></tt>.</p>
<p>In the method of a class-based view:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">reverse</span><span class="p">(</span><span class="s">&#39;polls:index&#39;</span><span class="p">,</span> <span class="n">current_app</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">request</span><span class="o">.</span><span class="n">resolver_match</span><span class="o">.</span><span class="n">namespace</span><span class="p">)</span>
</pre></div>
</div>
<p>and in the template:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">url</span> <span class="s1">&#39;polls:index&#39;</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>Note that reversing in the template requires the <tt class="docutils literal"><span class="pre">current_app</span></tt> be added as
an attribute to the template context like this:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">render_to_response</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">context</span><span class="p">,</span> <span class="o">**</span><span class="n">response_kwargs</span><span class="p">):</span>
    <span class="n">response_kwargs</span><span class="p">[</span><span class="s">&#39;current_app&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">request</span><span class="o">.</span><span class="n">resolver_match</span><span class="o">.</span><span class="n">namespace</span>
    <span class="k">return</span> <span class="nb">super</span><span class="p">(</span><span class="n">DetailView</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">render_to_response</span><span class="p">(</span><span class="n">context</span><span class="p">,</span> <span class="o">**</span><span class="n">response_kwargs</span><span class="p">)</span>
</pre></div>
</div>
</li>
<li><p class="first">If there is no current instance - say, if we were rendering a page
somewhere else on the site - <tt class="docutils literal"><span class="pre">'polls:index'</span></tt> will resolve to the last
registered instance of <tt class="docutils literal"><span class="pre">polls</span></tt>. Since there is no default instance
(instance namespace of <tt class="docutils literal"><span class="pre">'polls'</span></tt>), the last instance of <tt class="docutils literal"><span class="pre">polls</span></tt> that is
registered will be used. This would be <tt class="docutils literal"><span class="pre">'publisher-polls'</span></tt> since it&#8217;s
declared last in the <tt class="docutils literal"><span class="pre">urlpatterns</span></tt>.</p>
</li>
<li><p class="first"><tt class="docutils literal"><span class="pre">'author-polls:index'</span></tt> will always resolve to the index page of the instance
<tt class="docutils literal"><span class="pre">'author-polls'</span></tt> (and likewise for <tt class="docutils literal"><span class="pre">'publisher-polls'</span></tt>) .</p>
</li>
</ul>
<p>If there were also a default instance - i.e., an instance named <tt class="docutils literal"><span class="pre">'polls'</span></tt> -
the only change from above would be in the case where there is no current
instance (the second item in the list above). In this case <tt class="docutils literal"><span class="pre">'polls:index'</span></tt>
would resolve to the index page of the default instance instead of the instance
declared last in <tt class="docutils literal"><span class="pre">urlpatterns</span></tt>.</p>
</div>
</div>
<div class="section" id="s-url-namespaces-and-included-urlconfs">
<span id="s-namespaces-and-include"></span><span id="url-namespaces-and-included-urlconfs"></span><span id="namespaces-and-include"></span><h3>URL namespaces and included URLconfs<a class="headerlink" href="#url-namespaces-and-included-urlconfs" title="Permalink to this headline">¶</a></h3>
<p>URL namespaces of included URLconfs can be specified in two ways.</p>
<p>Firstly, you can provide the <a class="reference internal" href="#term-application-namespace"><em class="xref std std-term">application</em></a> and
<a class="reference internal" href="#term-instance-namespace"><em class="xref std std-term">instance</em></a> namespaces as arguments to
<a class="reference internal" href="../../ref/urls.html#django.conf.urls.include" title="django.conf.urls.include"><tt class="xref py py-func docutils literal"><span class="pre">include()</span></tt></a> when you construct your URL patterns. For
example,:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">url</span><span class="p">(</span><span class="s">r&#39;^polls/&#39;</span><span class="p">,</span> <span class="n">include</span><span class="p">(</span><span class="s">&#39;polls.urls&#39;</span><span class="p">,</span> <span class="n">namespace</span><span class="o">=</span><span class="s">&#39;author-polls&#39;</span><span class="p">,</span> <span class="n">app_name</span><span class="o">=</span><span class="s">&#39;polls&#39;</span><span class="p">)),</span>
</pre></div>
</div>
<p>This will include the URLs defined in <tt class="docutils literal"><span class="pre">polls.urls</span></tt> into the
<a class="reference internal" href="#term-application-namespace"><em class="xref std std-term">application namespace</em></a> <tt class="docutils literal"><span class="pre">'polls'</span></tt>, with the <a class="reference internal" href="#term-instance-namespace"><em class="xref std std-term">instance namespace</em></a>
<tt class="docutils literal"><span class="pre">'author-polls'</span></tt>.</p>
<p>Secondly, you can include an object that contains embedded namespace data. If
you <tt class="docutils literal"><span class="pre">include()</span></tt> a list of <a class="reference internal" href="../../ref/urls.html#django.conf.urls.url" title="django.conf.urls.url"><tt class="xref py py-func docutils literal"><span class="pre">url()</span></tt></a> instances,
the URLs contained in that object will be added to the global namespace.
However, you can also <tt class="docutils literal"><span class="pre">include()</span></tt> a 3-tuple containing:</p>
<div class="highlight-python"><div class="highlight"><pre>(&lt;patterns object&gt;, &lt;application namespace&gt;, &lt;instance namespace&gt;)
</pre></div>
</div>
<p>For example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.conf.urls</span> <span class="kn">import</span> <span class="n">include</span><span class="p">,</span> <span class="n">patterns</span><span class="p">,</span> <span class="n">url</span>

<span class="kn">from</span> <span class="nn">.</span> <span class="kn">import</span> <span class="n">views</span>

<span class="n">polls_patterns</span> <span class="o">=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">&#39;&#39;</span><span class="p">,</span>
    <span class="n">url</span><span class="p">(</span><span class="s">r&#39;^$&#39;</span><span class="p">,</span> <span class="n">views</span><span class="o">.</span><span class="n">IndexView</span><span class="o">.</span><span class="n">as_view</span><span class="p">(),</span> <span class="n">name</span><span class="o">=</span><span class="s">&#39;index&#39;</span><span class="p">),</span>
    <span class="n">url</span><span class="p">(</span><span class="s">r&#39;^(?P&lt;pk&gt;\d+)/$&#39;</span><span class="p">,</span> <span class="n">views</span><span class="o">.</span><span class="n">DetailView</span><span class="o">.</span><span class="n">as_view</span><span class="p">(),</span> <span class="n">name</span><span class="o">=</span><span class="s">&#39;detail&#39;</span><span class="p">),</span>
<span class="p">)</span>

<span class="n">url</span><span class="p">(</span><span class="s">r&#39;^polls/&#39;</span><span class="p">,</span> <span class="n">include</span><span class="p">((</span><span class="n">polls_patterns</span><span class="p">,</span> <span class="s">&#39;polls&#39;</span><span class="p">,</span> <span class="s">&#39;author-polls&#39;</span><span class="p">))),</span>
</pre></div>
</div>
<p>This will include the nominated URL patterns into the given application and
instance namespace.</p>
<p>For example, the Django admin is deployed as instances of
<a class="reference internal" href="../../ref/contrib/admin/index.html#django.contrib.admin.AdminSite" title="django.contrib.admin.AdminSite"><tt class="xref py py-class docutils literal"><span class="pre">AdminSite</span></tt></a>.  <tt class="docutils literal"><span class="pre">AdminSite</span></tt> objects have a <tt class="docutils literal"><span class="pre">urls</span></tt>
attribute: A 3-tuple that contains all the patterns in the corresponding admin
site, plus the application namespace <tt class="docutils literal"><span class="pre">'admin'</span></tt>, and the name of the admin
instance. It is this <tt class="docutils literal"><span class="pre">urls</span></tt> attribute that you <tt class="docutils literal"><span class="pre">include()</span></tt> into your
projects <tt class="docutils literal"><span class="pre">urlpatterns</span></tt> when you deploy an admin instance.</p>
<p>Be sure to pass a tuple to <tt class="docutils literal"><span class="pre">include()</span></tt>. If you simply pass three arguments:
<tt class="docutils literal"><span class="pre">include(polls_patterns,</span> <span class="pre">'polls',</span> <span class="pre">'author-polls')</span></tt>, Django won&#8217;t throw an
error but due to the signature of <tt class="docutils literal"><span class="pre">include()</span></tt>, <tt class="docutils literal"><span class="pre">'polls'</span></tt> will be the
instance namespace and <tt class="docutils literal"><span class="pre">'author-polls'</span></tt> will be the application namespace
instead of vice versa.</p>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../../contents.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">URL dispatcher</a><ul>
<li><a class="reference internal" href="#overview">Overview</a></li>
<li><a class="reference internal" href="#how-django-processes-a-request">How Django processes a request</a></li>
<li><a class="reference internal" href="#example">Example</a></li>
<li><a class="reference internal" href="#named-groups">Named groups</a><ul>
<li><a class="reference internal" href="#the-matching-grouping-algorithm">The matching/grouping algorithm</a></li>
</ul>
</li>
<li><a class="reference internal" href="#what-the-urlconf-searches-against">What the URLconf searches against</a></li>
<li><a class="reference internal" href="#captured-arguments-are-always-strings">Captured arguments are always strings</a></li>
<li><a class="reference internal" href="#specifying-defaults-for-view-arguments">Specifying defaults for view arguments</a></li>
<li><a class="reference internal" href="#performance">Performance</a></li>
<li><a class="reference internal" href="#syntax-of-the-urlpatterns-variable">Syntax of the urlpatterns variable</a></li>
<li><a class="reference internal" href="#error-handling">Error handling</a></li>
<li><a class="reference internal" href="#passing-strings-instead-of-callable-objects">Passing strings instead of callable objects</a></li>
<li><a class="reference internal" href="#the-view-prefix">The view prefix</a><ul>
<li><a class="reference internal" href="#multiple-view-prefixes">Multiple view prefixes</a></li>
</ul>
</li>
<li><a class="reference internal" href="#including-other-urlconfs">Including other URLconfs</a><ul>
<li><a class="reference internal" href="#captured-parameters">Captured parameters</a></li>
</ul>
</li>
<li><a class="reference internal" href="#passing-extra-options-to-view-functions">Passing extra options to view functions</a><ul>
<li><a class="reference internal" href="#passing-extra-options-to-include">Passing extra options to <tt class="docutils literal"><span class="pre">include()</span></tt></a></li>
</ul>
</li>
<li><a class="reference internal" href="#reverse-resolution-of-urls">Reverse resolution of URLs</a><ul>
<li><a class="reference internal" href="#examples">Examples</a></li>
</ul>
</li>
<li><a class="reference internal" href="#naming-url-patterns">Naming URL patterns</a></li>
<li><a class="reference internal" href="#url-namespaces">URL namespaces</a><ul>
<li><a class="reference internal" href="#introduction">Introduction</a></li>
<li><a class="reference internal" href="#reversing-namespaced-urls">Reversing namespaced URLs</a><ul>
<li><a class="reference internal" href="#id4">Example</a></li>
</ul>
</li>
<li><a class="reference internal" href="#url-namespaces-and-included-urlconfs">URL namespaces and included URLconfs</a></li>
</ul>
</li>
</ul>
</li>
</ul>

  <h3>Browse</h3>
  <ul>
    
      <li>Prev: <a href="index.html">Handling HTTP requests</a></li>
    
    
      <li>Next: <a href="views.html">Writing views</a></li>
    
  </ul>
  <h3>You are here:</h3>
  <ul>
      <li>
        <a href="../../index.html">Django 1.7.8.dev20150401230226 documentation</a>
        
          <ul><li><a href="../index.html">Using Django</a>
        
          <ul><li><a href="index.html">Handling HTTP requests</a>
        
        <ul><li>URL dispatcher</li></ul>
        </li></ul></li></ul>
      </li>
  </ul>

  <h3>This Page</h3>
  <ul class="this-page-menu">
    <li><a href="../../_sources/topics/http/urls.txt"
           rel="nofollow">Show Source</a></li>
  </ul>
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Apr 02, 2015</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="index.html" title="Handling HTTP requests">previous</a>
     |
    <a href="../index.html" title="Using Django" accesskey="U">up</a>
   |
    <a href="views.html" title="Writing views">next</a> &raquo;</div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>